//
//  BBClockCell.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/4.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
import SDWebImage
class BBClockCell: UITableViewCell {

    var day:BBDay?{
        didSet{
            timeLabel.text = day?.dayId
            let url = URL.init(string: (day?.clockImage) ?? "")
            clockImage.sd_setImage(with: url, placeholderImage: UIImage(named: "default_image"), options:SDWebImageOptions.init(rawValue: 1), completed: nil)
            let distanceK = Int(truncating: (day?.totalDistance ?? 0))/1000
            let distanceM = (Int(truncating: (day?.totalDistance ?? 0)) % 1000)/100
            distanceLabel.text = "\(distanceK).\(distanceM)km"
            usedTimeLabel.text = "\(Int(truncating: day?.totalTime ?? 0)/3600)h,\(Int(day?.totalTime ?? 0)/60)m,\(Int(truncating: day?.totalTime ?? 0)%60)s"
            timesLabel.text = "\(Int(truncating: day?.times ?? 0))times"
            desLabel.text = day?.clockDes
            //desLabel.sizeToFit()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style:style,reuseIdentifier:reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(backView)
        backView.addSubview(activityTypeLabel)
        backView.addSubview(timeLabel)
        backView.addSubview(shareBT)
        backView.addSubview(clockImage)
        backView.addSubview(distanceLabel)
        backView.addSubview(usedTimeLabel)
        backView.addSubview(timesLabel)
        backView.addSubview(desLabel)
        
        backView.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size:CGSize(width: UIScreen.main.bounds.size.width-20, height:420), offset: CGPoint(x: 10, y: 10))
        activityTypeLabel.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: backView, size: nil, offset: CGPoint(x: 10, y: 10))
        timeLabel.xmg_AlignInner(type: XMG_AlignType.TopCenter, referView: backView, size: nil, offset: CGPoint(x: 0, y: 10))
        shareBT.xmg_AlignInner(type: XMG_AlignType.TopRight, referView: backView, size: CGSize(width: 20, height: 20), offset: CGPoint(x: -10, y: 10))
        let width = UIScreen.main.bounds.size.width
        clockImage.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: activityTypeLabel, size: CGSize(width: width-40, height: 200), offset: CGPoint(x: 0, y: 10))
        distanceLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: clockImage, size: nil, offset: CGPoint(x: 0, y: 10))
        usedTimeLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: clockImage, size: nil, offset: CGPoint(x: 0, y: 10))
        timesLabel.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: clockImage, size: nil, offset: CGPoint(x: 0, y: 10))
        desLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: distanceLabel, size: CGSize(width: width-40, height: 120), offset: CGPoint(x: 0, y: 10))
    }
    
//    @objc private func share(){
//        var AlertCon = UIAlertController(title: "Clock", message: "Do you want to send it to link?", preferredStyle: UIAlertController.Style.alert)
//        var ActionSure = UIAlertAction(title: "Sure", style: UIAlertAction.Style.default) { (_) in
//
//        }
//        var ActionCancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) { (_) in
//
//        }
//        AlertCon.addAction(ActionSure)
//        AlertCon.addAction(ActionCancel)
//
//        
//    }
    
    private lazy var backView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 10.0
        view.layer.masksToBounds = false   //如果是true，阴影不显示
        view.layer.shadowColor = UIColor.lightGray.cgColor//UIColor.init(red: 235/255.0, green: 232/255.0, blue: 235/255.0, alpha: 1).cgColor
        view.layer.shadowOpacity = 1.0
        view.layer.shadowRadius = 5.0
        view.layer.shadowOffset = CGSize(width: 0, height: 0)
        return view
    }()
    
    private lazy var activityTypeLabel:UILabel={
        let label = UILabel()
        label.textColor = UIColor.init(red: 204.0/255.0, green: 204.0/255.0, blue: 204.0/255.0, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = "Clocked"
        return label
    }()
    
    private lazy var timeLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.init(red: 103.0/255.0, green: 103.0/255.0, blue: 103.0/255.0, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 16)
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    public lazy var shareBT:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "share"), for: UIControl.State.normal)
//        btn.addTarget(self, action: #selector(share), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    private lazy var clockImage:UIImageView = {
        let img = UIImageView()
        return img
    }()
    
    private lazy var distanceLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        return label
    }()
    
    private lazy var usedTimeLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        return label
    }()
    
    private lazy var timesLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        return label
    }()
    
    private lazy var desLabel:UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.init(red: 250.0/255.0, green: 250.0/255.0, blue: 250.0/255.0, alpha: 1)
        label.numberOfLines = 0
        label.textColor = UIColor.init(red: 103.0/255.0, green: 103.0/255.0, blue: 103.0/255.0, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 13)
        return label
    }()
}
